<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class Wxuser extends Model
{
    //
    protected $table = 'wxusers';
    protected $guarded  = [];

    public function addStar($num)
    {
        try{
            DB::beginTransaction();


            $wxuserStar = new WxuserStar();
            $wxuserStar->id_wxusers = $this->id_wxusers;
            $wxuserStar->num = $num;
            $wxuserStar->wxuser_num_before = $this->remain_stars;
            $wxuserStar->wxuser_num_after = $this->remain_stars + $num;
            $wxuserStar->save();

            $this->remain_stars += $num;
            $this->total_stars += $num;
            $this->save();

            DB::commit();
        }catch (Exception $e){
            DB::rollBack();
        }
    }

    public function GetCardRecord()
    {
        return $this->hasMany(WxuserWxcard::class,'id_wxusers','id');
    }

    public function Stars()
    {
        return $this->hasMany(WxuserStar::class,'id_wxusers','id');
    }
}
